package com.tianhui.consignortwofei.other.auth;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;

import com.baidu.ocr.ui.camera.CameraActivity;
import com.fgs.common.pictureSelector.SelectPicture;
import com.luck.picture.lib.entity.LocalMedia;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.photograph.IPhotographStyle;
import com.tianhui.consignortwofei.other.auth.state.IPictureProcessState;
import com.tianhui.consignortwofei.widget.AuthTipDialog;

import java.util.List;

/**
 * @author fgs
 * @date 2021/5/12
 * @description
 **/
public abstract class PictureStrategy {
    public static final int REQUEST_CODE = 222;

    public abstract void selectPhoto(Context context, SelectPhotoCallback callback);

    public abstract void onActivityResult(int requestCode, int resultCode, @Nullable Intent data);


    public abstract IPhotographStyle getPhotographStyle();

    public abstract IPictureProcessState getPictureProcessState();

    public abstract int initTipImageId();

    public void showPictureDialog(Context context, SelectPhotoCallback callback) {
        AuthTipDialog authTipDialog = new AuthTipDialog(context);
        authTipDialog.takePhotoClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authTipDialog.dismiss();
                photograph(context);
            }
        });
        authTipDialog.selectPhotoClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authTipDialog.dismiss();
                album(context, callback);
            }
        });
        authTipDialog.setImage(initTipImageId());
        authTipDialog.show();
    }

    //拍照
    private void photograph(Context context) {
        Intent intent = new Intent(context, CameraActivity.class);
        IPhotographStyle iPhotographStyle = getPhotographStyle();
        if (iPhotographStyle != null) {
            iPhotographStyle.initPhotographStyle(intent);
        }
        ((Activity) context).startActivityForResult(intent, REQUEST_CODE);
    }

    protected void album(Context context, SelectPhotoCallback callback) {

        SelectPicture selectPicture = new SelectPicture();
        selectPicture.setIsCarma(false);
        selectPicture.setCrop(true);
        selectPicture.selectSinglePicture((Activity) context, new SelectPicture.OnSelectResultCallback() {
            @Override
            public void selectResultCallback(List<LocalMedia> selectList) {

            }

            @Override
            public void selectResultPath(String path) {
                SelectPhotoInfo selectPhotoInfo = new SelectPhotoInfo();
                selectPhotoInfo.filePath = path;
                IPictureProcessState iPictureProcessState = getPictureProcessState();
                if (iPictureProcessState != null) {
                    iPictureProcessState.stateProcess(context, selectPhotoInfo, callback);
                } else {
                    callback.selectPhotoSuccess(selectPhotoInfo);
                }
            }

            @Override
            public void deletePictureCallback(List<LocalMedia> selectList, int position) {

            }
        });
    }

}
